﻿#include "Customer.h"
#include "Controller.h"

#include <QDebug>
#include <QRandomGenerator>


Customer::Customer(QObject *parent)
    : Runable{parent}
{
    buffer = Controller::getQueue();
}

void Customer::run()
{
    qDebug() << "Customer Running in:" << QThread::currentThreadId();

    int lastIdx = 0;
    while (isRunning) {
        QString msg = "";
        if (buffer->isEmpty())
          continue;
        msg = buffer->dequeue();
        // 次序冲突验证
        int idx = msg.split(' ')[0].toInt();
        if (idx != lastIdx + 1) {
            qDebug() << "Sequence conflict! Last:" << lastIdx << "Current:" << idx << "Content:" << msg;
        }
        lastIdx = idx;
        emit sendMsg("Take [" + msg + "]");

        QThread::msleep(QRandomGenerator::global()->bounded(10));
    }
}
